public class RepeatedSubstringPattern {

    public boolean repeatedSubstringPattern(String s) {
        int n = s.length();
        for (int i = 0; i < n / 2 + 1; i++) {
            if (n % (i + 1) == 0) {
                if (process(s.substring(0, i + 1), n / (i + 1)).equals(s)) return true;
            }
        }
        return false;
    }

    public String process(String s, int n) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < n; i++) {
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }

    public boolean repeatedSubstringPattern1(String s) {
        int n = s.length();
        StringBuffer temp = new StringBuffer(s);
        for (int i = 0; i < n; i++) {
            StringBuffer copy = new StringBuffer(temp);
            temp.append(temp.substring(0, i + 1));
            temp.delete(0, i + 1);
            if (temp.toString().equals(s)) {
                return true;
            } else {
                temp = new StringBuffer(copy);
            }
        }
        return false;
    }
}
